export const initalToken = (vm) => {

	uni.getStorage({
		key: 'token',
		success: (res) => {
			console.log('initalToken');
			vm.prototype.$token = res.data
			vm.prototype.$aplsjsonheader = {
				token: res.data,
				"content-Type": "application/json",
			}
			vm.prototype.$aplsFormDataheader = {
				token: res.data,
				"content-Type": "multipart/form-data",
			}
			console.log(vm.prototype.$token, 'vm.prototype.$token');

		},
		fail: () => {
			console.log(49984949);
			vm.prototype.$token = ''
			vm.prototype.$aplsjsonheader = {
				token: '',
				"content-Type": "application/json",
			}
			vm.prototype.$aplsFormDataheader = {
				token: '',
				"content-Type": "multipart/form-data",
			}
		}
	})
}

export function resetToken() {
	uni.removeStorage({
		key: 'token'
	})
	this.$token = ''
	this.$header = {
		token: undefined,
		"content-Type": "application/json",
	}
}

export function updateToken(token) {
	uni.setStorage({
		key: 'token',
		data: token
	})
	this.$token = token
	this.$aplsjsonheader = {
		token: token,
		"content-Type": "application/json",
	}
	console.log(this.$aplsjsonheader, 52);
}

export function errMessageToast(errMsg) {
	uni.showToast({
		title: errMsg,
		duration: 1000,
		icon: 'none'
	})
}
export function back() {
	let pages = getCurrentPages()
	console.log(pages, 214);
	uni.navigateBack({
		delta: 1 // 返回的页面数
	})

}

export function setTimeoutback(type) {
	let pages = getCurrentPages(); // 当前页面
	let beforePage = pages[pages.length - 2]; // 前一个页面
	if (type && type == 'reload' && beforePage) {

		setTimeout(() => {
			uni.navigateBack({
				delta: 1,
				success: () => {
					console.log('beforePage');
					beforePage.$vm.reloadPage(); // 执行前一个页面的刷新
				}
			});
		}, 1200)

	} else {
		setTimeout(() => {
			uni.navigateBack({
				delta: 1 // 返回的页面数
			})
		}, 1200)
	}


}
export function saveStorage(key, value) {
	uni.setStorage({
		key: key,
		data: value
	})

}

export function removeStorage(key) {
	uni.removeStorage({
		key: key
	})

}

export function computedDownTime(time) {

	let setTime = new Date(time)
	let nowTime = new Date()

	let resetSec = setTime.getTime() - nowTime.getTime()

	let day = parseInt(resetSec / (60 * 60 * 24 * 1000))

	// var d = Math.floor(resetSec / 1000 / 60 / 60 / 24);
	// var h = Math.floor(resetSec / 1000 / 60 / 60 % 24);
	// var m = Math.floor(resetSec / 1000 / 60 % 60);
	// var s = Math.floor(resetSec / 1000 % 60);
	// var html = d + " 天" + h + " 时" + m + " 分" + s + " 秒";
	// console.log(html);
	return String(day)
}
export function institute(instituteType) {
	let instituteTypeDesc = ''
	switch (instituteType) {
		case 0:
			instituteTypeDesc = '文科'
			break;
		case 1:
			instituteTypeDesc = '理科'
			break;
		case 2:
			instituteTypeDesc = '专业不限'
			break;
	}

	return instituteTypeDesc
}

export function binduserInfo(res) {
	this.$userInfo = res
}
export function varitfyToken() {
	if (this.$token == "" || this.$token == undefined) {
		console.log(111);
		return false
	}
	if (this.$aplsjsonheader == "" || this.$aplsjsonheader == undefined) {
		console.log(222);
		return false
	}
	if (this.$aplsjsonheader.token == "" || this.$aplsjsonheader.token == undefined) {
		console.log(333);
		return false
	}
	let token = uni.getStorageSync('token') || ''
	if (!token) {
		return false
	}
	return true
}
export function timestampToTime(timestamp) {
	var date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
	var Y = date.getFullYear() + '-';
	var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
	var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
	var h = (date.getHours() < 10 ? '0' + (date.getHours()) : date.getHours()) + ':';
	var m = (date.getMinutes() < 10 ? '0' + (date.getMinutes()) : date.getMinutes()) + ':';
	var s = (date.getSeconds() < 10 ? '0' + (date.getSeconds()) : date.getSeconds());
	return Y + M + D + h + m + s;
}
export function subStrName(name, type) {
	// 名字脱敏
	switch (type) {
		case 'small':
			if (null != name && name != undefined) {

				if (name.length == 1) {
					return name
				} else if (name.length == 2) {
					return name.substr(1, 1)
				} else if (name.length == 3) {
					return name.substr(2, 1)
				} else {
					return name.substr(name.length - 1, 1)
				}
			} else {
				return '无'
			}
			break;


		case 'big':
			if (null != name && name != undefined) {

				if (name.length == 1) {
					return name
				} else if (name.length == 2) {
					return name.substr(1, 1)
				} else if (name.length == 3) {
					return name.substr(1, 2)
				} else {
					return name.substr(1, 2)
				}
			} else {
				return '用户'
			}
			break;
		case 'verybig':
			if (null != name && name != undefined) {
				console.log(name, 206);
				if (name.length == 1) {
					return name
				} else if (name.length == 2) {
					return name.substr(1, 1)
				} else if (name.length == 3) {
					return name.substr(1, 2)
				} else {
					return name.substr(1, 2)
				}
			} else {
				return '用户'
			}
			break;

	}
}
